//
//  SecondViewController.m
//  CoreData通讯录
//
//  Created by 邝子涵 on 15/11/21.
//  Copyright © 2015年 kuangzihan. All rights reserved.
//

#import "SecondViewController.h"
#import "LTView.h"
#import "CoreDataManager.h"
@interface SecondViewController ()
@property(nonatomic, strong) LTView *nameView;
@property(nonatomic, strong) LTView *sexView;
@property(nonatomic, strong) LTView *numberView;
@property(nonatomic, strong) LTView *phoneView;
@property(nonatomic, strong) LTView *qqView;
@property(nonatomic, strong) LTView *nativePlaceView;

@property(nonatomic, strong) CoreDataManager *manager;
@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.hidden = NO;
    
    self.manager = [CoreDataManager defaults];
    NSLog(@"url  ======= %@", [self.manager applicationDocumentsDirectory]);
    
    // 用LTView 来创建视图
    _nameView = [[LTView alloc] initWithFrame:CGRectMake(0, 0, 375, 50)];
    [self.view addSubview:_nameView];
    _nameView.myLabel.text = @"姓名:";
    
    _phoneView = [[LTView alloc] initWithFrame:CGRectMake(0, 50, 375, 50)];
    [self.view addSubview:_phoneView];
    _phoneView.myLabel.text = @"电话:";
    
    _numberView = [[LTView alloc] initWithFrame:CGRectMake(0, 100, 375, 50)];
    [self.view addSubview:_numberView];
    _numberView.myLabel.text = @"学号:";
    
    _sexView = [[LTView alloc] initWithFrame:CGRectMake(0, 150, 375, 50)];
    [self.view addSubview:_sexView];
    _sexView.myLabel.text = @"性别:";
    
    _qqView = [[LTView alloc] initWithFrame:CGRectMake(0, 200, 375, 50)];
    [self.view addSubview:_qqView];
    _qqView.myLabel.text = @"QQ:";
    
    _nativePlaceView = [[LTView alloc] initWithFrame:CGRectMake(0, 250, 375, 50)];
    [self.view addSubview:_nativePlaceView];
    _nativePlaceView.myLabel.text = @"籍贯:";
    
    
    
    UIButton *addButton = [UIButton buttonWithType:UIButtonTypeSystem];
    addButton.frame = CGRectMake(157.5, 320, 60, 30);
    addButton.layer.borderWidth = 1;
    addButton.layer.cornerRadius = 5;
    [addButton setTitle:@"添加" forState:UIControlStateNormal];
    [self.view addSubview:addButton];
    [addButton addTarget:self action:@selector(addButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(reKeyBoard:)];
    [self.view addGestureRecognizer:tap];
}

#pragma mark - 点击键盘回收键盘
- (void)reKeyBoard:(UITapGestureRecognizer *)tap
{
    // textField 失去第一相应者
    [_nameView.myTextField resignFirstResponder];
    [_sexView.myTextField resignFirstResponder];
    [_qqView.myTextField resignFirstResponder];
    [_nativePlaceView.myTextField resignFirstResponder];
    [_phoneView.myTextField resignFirstResponder];
    [_numberView.myTextField resignFirstResponder];
}

#pragma mark 添加联系人button点击方法
- (void)addButtonAction:(UIButton *)button{
    [self.navigationController popToRootViewControllerAnimated:YES];
    
    [self.secondDelegate addAressBook:[[CoreDataManager  defaults] addDataAddressBook:_nameView.myTextField.text phone:_phoneView.myTextField.text sex:_sexView.myTextField.text number:[NSNumber numberWithInteger:[_numberView.myTextField.text integerValue]] QQ:_qqView.myTextField.text nativePlace:_nameView.myTextField.text]];
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
